﻿using System;
using System.Collections.Generic;
using System.Data.SqlClient;
using System.Text;

namespace DataManager.DAL
{
    public struct Employee
    {
        int m_EmployeeId;
        string m_EmployeeName;
        string m_Address;
        int m_Salar;
        string m_UserName;
        string m_Password;
        bool m_Admin;

        public int EmployeeId
        {
            get { return m_EmployeeId; }
            set { m_EmployeeId = value; }
        }

        public string EmployeeName
        {
            get { return m_EmployeeName; }
            set { m_EmployeeName = value; }
        }

        public string Address
        {
            get { return m_Address; }
            set { m_Address = value; }
        }

        public int Salar
        {
            get { return m_Salar; }
            set { m_Salar = value; }
        }

        public string UserName
        {
            get { return m_UserName; }
            set { m_UserName = value; }
        }
        public string Password
        {
            get { return m_Password; }
            set { m_Password = value; }
        }

        public bool Admin
        {
            get { return m_Admin; }
            set { m_Admin = value; }
        }
    }

    class Employees:DAL
    {
        /// <summary>
        /// Visszateriti az alkalmazottak listajat.
        /// </summary>
        /// <returns></returns>
        public List<Employee> GetEmployeeList()
        {
            string query = "SELECT AlkalmazottId, AlkalmazottNev, Cim, Fizetes, FelhasznaloNev, Jelszo, Admin FROM Alkalmazottak";
            string error = string.Empty;
            SqlDataReader rdr = ExecuteReader(query, ref error);
            List<Employee> employeeList = new List<Employee>();
            if (error == "OK")
            {
                while (rdr.Read())
                {
                    Employee item = new Employee();
                    item.EmployeeId = Convert.ToInt32(rdr["AlkalmazottId"]);
                    item.EmployeeName = rdr["AlkalmazottNev"].ToString();
                    item.Address = rdr["Cim"].ToString();
                    item.Salar = Convert.ToInt32(rdr["Fizetes"]);
                    item.UserName = rdr["FelhasznaloNev"].ToString();
                    item.Password = rdr["Jelszo"].ToString();
                    int temp = Convert.ToInt32(rdr["Admin"]);
                    if (temp == 0)
                    {
                        item.Admin = false;
                    }
                    else
                    {
                        item.Admin = true;
                    }
                    employeeList.Add(item);
                }
            }
            CloseDataReader(rdr);
            return employeeList;
        }

        /// <summary>
        /// Visszateriti egy alkalmazott Idjat a megadott felhasznalonev alapjan
        /// Ez a bejelentkezesnel hasznalom
        /// </summary>
        /// <param name="userName"></param>
        /// <param name="password"></param>
        /// <param name="admin"></param>
        /// <returns></returns>
        public int getEmployeeId(string userName, string password, int admin)
        {
            string query = "SELECT AlkalmazottId " +
                "FROM Alkalmazottak " +
                "WHERE FelhasznaloNev = '" + userName + "' " +
                "AND Jelszo = '" + password + "' " + 
                "AND Admin = " + admin;
            string error = string.Empty;
            int id = 0;
            SqlDataReader rdr = ExecuteReader(query, ref error);
            if (error == "OK")
            {
                if (rdr.HasRows)
                {
                    while (rdr.Read())
                    {
                        id = Convert.ToInt32(rdr[0]);
                    }
                }
            }
            CloseDataReader(rdr);
            return id;
        }
    }
}
